package com.canbot.u05.activity.attendance;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.AttendanceSettings;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.bean.KqGroup;
import com.canbot.u05.bean.PersonGroupData;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 考勤分组选择
 */
public class AttendanceInitActivity extends BaseActivityNoTitle {

        private static final String TAG = "AttendanceInitActivity";

        private String groupId;

        private Dialog dialogLoading;

        private AttendanceSettings attendanceSettings;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_attendance_init);

                getDefaultSetting();

        }


        /**
         * 获取之前设置的时间（网络获取）
         */
        private void getDefaultSetting() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.attendance_load_tip));
                HashMap<String,String> map = new HashMap<>();
                map.put("type","get");
                OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + ConstUtils.URL_ATTENDANCE_SET ,map, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(final String result) {

                                Logger.e(TAG, "onSuccess:" + result);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                JsonResponse jc = JSON.parseObject(result, JsonResponse.class);
                                                attendanceSettings = JSON.parseObject(jc.getResult().toString(),AttendanceSettings.class);
                                                groupId = attendanceSettings.getGroupId();
                                                if (TextUtils.isEmpty(groupId)){
                                                        getGroup();
                                                }else {
                                                        Intent intent = new Intent(AttendanceInitActivity.this,AttendanceSettingTimeActivity.class);
                                                        intent.putExtra("settings",attendanceSettings);
                                                        startActivity(intent);
                                                        finish();
                                                }

                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                ToastTools.newInstance(AttendanceInitActivity.this).showText(AttendanceInitActivity.this.getString(R.string.network_error));
                                        }
                                });
                        }
                });



        }


        /**
         * 获取所有人脸分组
         */
        private void getGroup() {

                HashMap<String, Object> hasMap = new HashMap<>();
                hasMap.put("group","");
                OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + ConstUtils.URL_GET_FACE_OR_GROUP, hasMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(final String result) {

                                Logger.e(TAG, "onSuccess:" + result);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {

                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                                if (strMsgBean.getRc() == 0) {
                                                        List<PersonGroupData> personGroup = JSON.parseArray(strMsgBean.getResult().toString(), PersonGroupData.class);
                                                        if (personGroup != null && personGroup.size() != 0) {
                                                                List<KqGroup> data = new ArrayList<KqGroup>();
                                                                KqGroup kqGroup;
                                                                for (int i = 0; i < personGroup.size(); i++) {
                                                                        kqGroup = new KqGroup();
                                                                        kqGroup.setGroupId(personGroup.get(i).getId());
                                                                        kqGroup.setGroupName(personGroup.get(i).getName());
                                                                        data.add(kqGroup);
                                                                }
                                                                Logger.e(TAG,"group_data = " +data.toString());
                                                                Intent intent = new Intent(AttendanceInitActivity.this,AttendanceGroupActivity.class);
                                                                intent.putExtra("settings",attendanceSettings);
                                                                intent.putExtra("group_data",JSON.toJSONString(data));
                                                                startActivity(intent);
                                                                finish();
                                                        }
                                                }
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                ToastTools.newInstance(AttendanceInitActivity.this).showText(AttendanceInitActivity.this.getString(R.string.network_error));
                                        }
                                });
                        }
                });
        }


        @Override
        protected void onPause() {
                if (dialogLoading != null && dialogLoading.isShowing()){
                        LoadingDialogUtils.closeDialog(dialogLoading);
                }
                super.onPause();
        }
}
